<template>
    <div class="scoring-box">
        <h3>{{ title }}</h3>
        <div class="score-box">
            <div class="star-box">
                <img
                    v-for="(item, index) in starImgList"
                    :key="index"
                    :src="require('../assets/images//logistics/' + item)"
                    @click="flag ? changeStar(index + 1) : ''"
                />
            </div>
            <div class="score">{{ showScore ? computScore + '分' : '' }}</div>
        </div>
    </div>
</template>

<script>
export default {
    props: ['title', 'score', 'flag', 'ifSuccess', 'showScore'],
    data() {
        return {
            starImgList: ['score-gray.png', 'score-gray.png', 'score-gray.png', 'score-gray.png', 'score-gray.png'],
            tempIndex: 5,
        };
    },
    methods: {
        changeStar(index) {
            let tempColor = '';
            this.tempIndex = index;
            if (this.tempIndex <= 2) {
                tempColor = 'red';
            } else if (this.tempIndex <= 4) {
                tempColor = 'orange';
            } else {
                tempColor = 'yellow';
            }
            this.starImgList = this.starImgList.map(() => {
                return 'score-gray.png';
            });
            for (let i = 0; i < index; i++) {
                this.starImgList[i] = `score-${tempColor}.png`;
            }
            this.$emit('changeStar', this.tempIndex);
        },
    },
    created() {
        this.changeStar(this.tempIndex);
    },
    watch: {
        ifSuccess(flag) {
            if (flag) {
                this.tempIndex = this.score;
                this.changeStar(this.tempIndex);
            }
        },
    },
    computed: {
        computScore() {
            return this.tempIndex;
        },
    },
};
</script>

<style lang="less" scoped>
.scoring-box {
    display: flex;
    justify-content: space-around;
    align-items: center;
    &:nth-of-type(2) {
        padding: 0.3rem 0;
        margin: 0.3rem 0;
        border: 0.01rem dashed #e5e5e5;
        border-left: none;
        border-right: none;
    }
    h3 {
        color: #333;
        font-weight: 500;
        margin-left: 0.2rem;
    }
    .score-box {
        display: flex;
        align-items: center;
        justify-content: space-around;
        img {
            width: 0.51rem;
            margin-right: 0.14rem;
        }
        .score {
            width: 1rem;
            text-align: right;
        }
    }
}
</style>
